package com.example.selfservicestation.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.selfservicestation.R
import com.example.selfservicestation.base.BaseBindingFragment
import com.example.selfservicestation.constant.DEFAULT_LOCK_COUNT
import com.example.selfservicestation.constant.EXTRA_LOCK_ON_NUM
import com.example.selfservicestation.constant.SP_LOCK_COUNT
import com.example.selfservicestation.constant.TIME_FULL_FORMAT
import com.example.selfservicestation.databinding.FragmentPocketSuccessBinding
import com.example.selfservicestation.extensions.safeNavigate
import com.example.selfservicestation.global.GlobalParams
import com.example.selfservicestation.singleton.SharedPreferenceUtil
import com.example.selfservicestation.util.Executor
import com.example.selfservicestation.util.Tip
import com.example.selfservicestation.util.formatCurrentTime
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class PocketSuccessFragment: BaseBindingFragment<FragmentPocketSuccessBinding>(FragmentPocketSuccessBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    @SuppressLint("SetTextI18n")
    private fun initViews(){
        GlobalParams.isFlowFinish = false
        binding.layoutSumPocket.tvResultNumber.text = "0"
        binding.layoutSumPocket.tvResultUnit.text = "个"
        binding.layoutSumPocket.tvName.text = "累计领袋"

        binding.layoutSumIncome.tvResultNumber.text = "0"
        binding.layoutSumIncome.tvResultUnit.text = "元"
        binding.layoutSumIncome.tvName.text = "累计收益"

        binding.layoutSumEnvironment.tvResultNumber.text = "1"
        binding.layoutSumEnvironment.tvResultUnit.text = "个"
        binding.layoutSumEnvironment.tvName.text = "已领环保袋"

        binding.layoutSumRecyclable.tvResultNumber.text = "0"
        binding.layoutSumRecyclable.tvResultUnit.text = "个"
        binding.layoutSumRecyclable.tvName.text = "循环利用环保袋"

        binding.tvOrderNo.text = "订单编号：${System.currentTimeMillis()}"
        binding.tvPocketTime.text = "取袋时间：${formatCurrentTime(TIME_FULL_FORMAT)}"

        binding.ivGif.setImageResource(R.mipmap.gif_success)

        binding.tvPhoneName.text = GlobalParams.loginPhoneOrCard

        arguments?.getInt(EXTRA_LOCK_ON_NUM)?.let {
            binding.tvPocketNo.text = it.toString()
            Tip.speakNum(it.toString(), null){
                Executor.delayOnMain(1000){
                    Tip.audio(R.raw.lock_open)
                }
            }
        }

        binding.btnExit.setOnClickListener {
            findNavController().safeNavigate(R.id.pocketSuccessFragment_to_homeFragment)
        }

        viewLifecycleOwner.lifecycleScope.launch {
            (29 downTo 1).asFlow().onEach { delay(1000) }.onCompletion {
                findNavController().safeNavigate(R.id.pocketSuccessFragment_to_homeFragment)
            }.collect{
                binding.btnExit.text = "立即退出（${it}s）"
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        GlobalParams.isFlowFinish = true
    }
}